package pageroute

import (
	"time"

	"gioui.org/layout"
	"gioui.org/widget/material"
	"git.sr.ht/~mil/transito/connmobroute"
)

func wgButtonRoute(widget *PageRoute, gtx layout.Context) layout.Dimensions {
	if widget.clickableRoute.Clicked() {
		go func() {
			now := time.Now()
			widget.TimerStartMobroute = &now
			defer func() {
				widget.TimerStartMobroute = nil
				widget.UIState.Window.Invalidate()
			}()
			mobrouteResult, err := connmobroute.MobrouteExecuteRoute(connmobroute.MobrouteRequest{
				Sources:            widget.UIState.Sources,
				MaxWalkMinutes:     widget.textfieldMaxWalkMinutes.Text(),
				MaxTripMinutes:     widget.textfieldMaxTripMinutes.Text(),
				MinTransferMinutes: widget.textfieldMinTransferMinutes.Text(),
				MaxTransferMinutes: widget.textfieldMaxTransferMinutes.Text(),
				WalkspeedKmHr:      widget.textfieldWalkspeedKmHr.Text(),
				LatLonFrom:         widget.UIState.LocationFromCoords,
				LatLonTo:           widget.UIState.LocationToCoords,
				TransfersTable:     widget.UIState.TransfersTable,
				TransfersGenerated: widget.UIState.TransfersComputed,
			})
			widget.ResultMobrouteResponse = mobrouteResult
			widget.ResultMobrouteError = err
		}()
	}
	return material.Button(widget.UIState.Theme, &widget.clickableRoute, "Route").Layout(gtx)
}
